package com.jak.demo.Thread;

import java.util.Random;
import java.util.concurrent.*;

public class TestScheduledThreadPool {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
        // 五个线程执行，有重复使用的
        for (int i = 0; i < 10; i++) {
            Future<Integer> future = pool.schedule(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int num = new Random().nextInt(100);//生成随机数
                    System.out.println(Thread.currentThread().getName() + ":" + num);
                    return num;
                }
            }, 2, TimeUnit.SECONDS); // 每隔2秒钟执行一次，时间间隔，时间单位
            System.out.println(future.get());
        }
        pool.shutdown();
    }
}
